
package jerry;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import jerry.html.RemoveTrailingBytes;

/**
 * UT encrypted world logs to simple log
 * converter
 * 
 * @author (h0t@_G0|i, Alby87
 *
 */
public class ConvertUTWorldLogToSimpleText {
	
	/**
	 * Variable to mask the bits in order to extract 
	 * actual data from encrypted data.
	 */
	public static final byte PROCESSOR = (byte)167;
	
	/**
	 * Exposed to call from command line if that
	 * be the case.
	 * 
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		System.out.println("Enter directory where logs will be generated : ");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String fileDir = br.readLine();
		br.close();
		for (String files : args) {
			convertFile(files,fileDir);
		}
	}
	
	/**
	 * Utility method to be called from the log processor method
	 * 
	 * @param args files list
	 * @param fileDir directory to convert files
	 * @throws IOException
	 */
	public static String convertFile(String logFileName, String fileDir) throws IOException {
		byte[] buffer = new byte[9000];
		int available = 0;
		File f = new File(logFileName);
		FileInputStream stream = new FileInputStream(f);
		String output=fileDir + File.separatorChar + f.getName() + ".temp";
		FileOutputStream outStream = new FileOutputStream(new File(output));
		while(true) {
			available = stream.read(buffer);
			if(available == -1){
				break;
			}
			byte [] writebuffer = new byte[available];
			for(int i = 0; i<available; i++){
				writebuffer[i] = (byte)(buffer[i]^PROCESSOR);
			}
			outStream.write(writebuffer);
			outStream.flush();
		}
		outStream.close();
		stream.close();
		f.delete();
		output = RemoveTrailingBytes.clearUp(output);
        return output;
	}
} 